package com.example.chat.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chat.beans.Background;
import com.example.chat.beans.Banner;
import com.example.chat.beans.Video;
import com.example.chat.beans.VideoType;
import com.example.chat.config.StatusEnums;
import com.example.chat.mapper.AdminMapper;
import com.example.chat.mapper.BannerMapper;
import com.example.chat.mapper.VideoTypeMapper;
import com.example.chat.result.ApiResult;
import com.example.chat.result.Base;
import com.example.chat.result.ListResult;
import com.example.chat.result.VideoResult;
import com.example.chat.utils.CheckUtils;
import com.example.chat.utils.Global;
import com.example.chat.utils.OssUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class BannerService extends ServiceImpl<BannerMapper, Banner> {
    @Resource
    CheckUtils checkUtils;

    public ApiResult<Base> updateBanner(Integer userId, String token, long id, String title, String img, String linkUrl,
                                        Integer videoId, int type, int status) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "");
        if (apiResult.getStatus() != 1){
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);}
        Banner banner = baseMapper.selectById(id);
        if (null != banner) {
            ApiResult<Base> result = new ApiResult<>();
            int resultStatus = 1;
            String msg = "修改成功";
            if (status == 2) {
                msg = "下架成功";
                banner.setStatus(status);
            } else {
                banner.setTitle(title);
                banner.setType(type);
                banner.setImg(img);
                banner.setLinkUrl(linkUrl);
                if (ObjectUtils.isNotEmpty(videoId)) {
                    banner.setVideoId(videoId);
                }
            }
            updateById(banner);
            result.setStatus(resultStatus);
            result.setMsg(msg);
            return result;
        } else {
            return new ApiResult<>(0, "banner不存在!", null);
        }
    }

    public ApiResult<Base> addBanner(Integer userId, String token, String title, String img, String linkUrl,
                                     Integer videoId, int type, int status) {

        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "");
        if (apiResult.getStatus() != 1){
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);}
        ApiResult<Base> result = new ApiResult<>();
        int resultStatus = 0;
        String msg = "创建失败";
        try {
            Banner banner = new Banner();
            banner.setTitle(title);
            banner.setType(type);
            banner.setStatus(status);
            banner.setImg(img);
            banner.setLinkUrl(linkUrl);
            if (ObjectUtils.isNotEmpty(videoId)) {
                banner.setVideoId(videoId);
            }
            banner.setCreateTime(new Date());
            baseMapper.insert(banner);
            resultStatus = 1;
            msg = "创建成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(resultStatus);
        result.setMsg(msg);
        return result;
    }

    public ListResult<List<Banner>> getBannerList(Integer page, Integer limit, String title, Integer type) {

        ListResult<List<Banner>> result = new ListResult<>();
        int Resultstatus = 0;
        String msg = "获取失败";
        try {
            Page<Banner> pages = new Page<>(page, limit);
            IPage<Banner> iPages;
            iPages = baseMapper.getBannerList(pages, title, type);
            result.setData(iPages.getRecords());
            result.setPage(page);
            result.setLimit(limit);
            result.setTotal(iPages.getTotal());
            msg = "获取成功";
            Resultstatus = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(Resultstatus);
        result.setMsg(msg);
        return result;

    }

    public ApiResult<List<Banner>> bannerForA() {
        ApiResult<List<Banner>> result = new ApiResult<>();
        int resultStatus = 0;
        String msg = "获取失败";
        try {
            QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", 1);
            List<Banner> banners = baseMapper.selectList(queryWrapper);
            result.setData(banners);
            msg = "获取成功";
            resultStatus = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(resultStatus);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> deleteBanner(Integer userId, String token, Integer id) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "1104");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "删除失败";
        try {
            Banner banner = baseMapper.selectById(id);
            if (banner == null) {
                return new ApiResult<>(0, "banner不存在！", null);
            }
            baseMapper.deleteById(id);
            msg = "删除成功";
            status = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }
}
